<template>
  <view>
    <!-- 1. 头部搜索框 -->
    <ugo-search />
    <!-- 2. tabs 选项卡 -->
    <u-tabs :list="tabList" lineColor="#EB4450" :scrollable="false"></u-tabs>
    <!-- 3. 商品列表 -->
    <view class="goods-list">
      <!-- 商品列表渲染 -->
      <navigator
        class="goods-item"
        v-for="item in goodsList"
        :key="item.goods_id"
        :url="`/pages/goods_detail/index?goods_id=${item.goods_id}`"
      >
        <image
          :src="item.goods_small_logo || defaultSrc"
          lazy-load
          class="goods-image"
          mode="aspectFit"
        />
        <!-- 商品信息 -->
        <view class="goods-info">
          <view class="goods-name">{{ item.goods_name }}</view>
          <view class="goods-price">{{ item.goods_price }}</view>
        </view>
      </navigator>
    </view>
    <!-- 底部提示 -->
    <view
      class="m-tips"
      v-if="total && total === goodsList.length"
      style="line-height: 2; font-size: 24rpx; color: #999; text-align: center"
      >没有更多数据了</view
    >
  </view>
</template>

<script>
export default {
  data() {
    return {
      queryParams: {},
      goodsList: [],
      total: 0,
      tabList: [{ name: "综合" }, { name: "销量" }, { name: "价格" }],
      defaultSrc:
        "https://img0.baidu.com/it/u=324754129,1809452373&fm=253&fmt=auto&app=138&f=JPEG?w=529&h=500",
    };
  },
  onLoad(options) {
    console.log(options);
    this.queryParams = {
      ...options,
      pagenum: 1,
      pagesize: 20,
    };
    this.getGoodsList();
  },
  // 下拉刷新钩子
  async onPullDownRefresh() {
    // 重置数据
    this.queryParams.pagenum = 1;
    this.goodsList = [];
    this.total = 0;
    // 重新发请求
    await this.getGoodsList();

    uni.stopPullDownRefresh();
  },
  // 触底钩子事件
  onReachBottom() {
    // console.log("到底了~");
    // 判断有没有下一页数据
    // 有
    if (this.goodsList.length < this.total) {
      // 页码加1
      this.queryParams.pagenum++;
      this.getGoodsList();
    } else {
      uni.showToast({
        title: "我也是有底线的~",
        icon: "none",
      });
    }
  },
  methods: {
    async getGoodsList() {
      const res = await uni.$u.http.get("/goods/search", {
        params: this.queryParams,
      });
      // console.log(res);
      // 将请求回来数据与原有数据合并
      // this.goodsList = [...this.goodsList, ...res.data.message.goods];
      this.goodsList.push(...res.data.message.goods);
      this.total = res.data.message.total;
    },
  },
};
</script>

<style lang="scss">
.goods-list {
  .goods-item {
    display: flex;
    border-bottom: 1rpx solid #ddd;
    padding: 25rpx;
    .goods-image {
      width: 190rpx;
      height: 190rpx;
    }
    .goods-info {
      flex: 1;
      font-size: 24rpx;
      /* 垂直方向两端对齐 */
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 20rpx;
      .goods-name {
        /* 多行超出隐藏 */
        overflow: hidden;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }
      .goods-price {
        color: #eb4450;
        /* 前面的价格符号 */
        &::before {
          content: "￥";
          font-size: 80%;
          margin-right: 5rpx;
        }
      }
    }
  }
}
</style>